﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebService_Viewer : System.Web.UI.Page
{
    public enum ViewerType
    {
        XML,
        Text
    }
    public string Content;
    public ViewerType ViewType = ViewerType.XML;
    protected void Page_Load(object sender, EventArgs e)
    {
        string type = Request["type"];
        if (string.IsNullOrEmpty(type))
        {
            if (Request.Form.Count > 0)
            {
                string contentType = "viewer";
                foreach (string key in Request.Form.AllKeys)
                {
                    if (key.StartsWith("result-"))
                    {
                        Content = Request.Form[key];
                    }
                    else if (key.StartsWith("type-"))
                    {
                        contentType = Request.Form[key];
                    }
                }
                if (contentType != "viewer")
                {
                    Response.Clear();
                    Response.ContentType = contentType;
                    Content = System.Web.HttpUtility.UrlDecode(Content);
                    Response.Write(Content);
                    Response.End();
                }
                if (contentType == "viewer" && !string.IsNullOrEmpty(Content))
                {
                    Content = System.Web.HttpUtility.UrlDecode(Content);
                    Content = System.Web.HttpUtility.HtmlEncode(Content);
                }
            }
            else
            {
                Response.Clear();
                Response.Write("Invalid parameter(No request found!)");
                Response.End();
            }
        }
        else
        {
            type = type.Trim().ToUpper();
            if (type == "XML")
                ViewType = ViewerType.XML;
            else if (type == "TEXT")
                ViewType = ViewerType.Text;
            else
                ViewType = ViewerType.XML;
            if (string.IsNullOrEmpty(Content))
            {
                Content = Request.Form["result-text-xml"];
            }
        }
        if (!string.IsNullOrEmpty(Request["pn"]))
        {
            string path = GetTogether.Studio.Setting.GetIncludeFolder();
            path = System.IO.Path.Combine(path, "Viewer");
            path = System.IO.Path.Combine(path, Request["share-usercode"]);
            if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);
            string file = path + "\\" + Request["pn"] + ".xml";
            Content = System.IO.File.ReadAllText(file, System.Text.Encoding.UTF8);
            Content = System.Web.HttpUtility.HtmlEncode(Content);
        }
    }
}